package cn.funeralobjects.ams.web.controller;

import cn.funeralobjects.ams.entity.EProductApi;
import cn.funeralobjects.ams.model.ProductApi;
import cn.funeralobjects.ams.service.BaseAmsService;
import cn.funeralobjects.ams.service.ProductApiAccessService;
import cn.funeralobjects.ams.service.ProductApiService;
import cn.funeralobjects.ams.web.api.ProductApiApi;
import cn.funeralobjects.ams.web.mapper.ProductApiMapper;
import cn.funeralobjects.ams.web.model.MProductApi;
import cn.funeralobjects.ams.web.model.VProductApi;
import cn.funeralobjects.ams.web.model.VProductApiAccessDto;
import cn.funeralobjects.common.service.NameService;
import cn.funeralobjects.common.web.controller.RestNameController;
import cn.funeralobjects.common.web.controller.converter.ModelMapper;
import cn.funeralobjects.util.ReactorUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

/**
 * @author FuneralObjects
 * Create date: 2020/5/28 6:02 PM
 */
@RestController
@RequestMapping("/product-api")
public class ProductApiApiController implements ProductApiApi, BaseAmsController<VProductApi, MProductApi, ProductApi, EProductApi, Integer>, RestNameController<VProductApi, EProductApi, Integer> {

    @Resource
    private ProductApiService productApiService;

    @Resource
    private ProductApiMapper productApiMapper;

    @Resource
    private ProductApiAccessService productApiAccessService;

    @Override
    public BaseAmsService<ProductApi, EProductApi, Integer> baseAmsService() {
        return productApiService;
    }

    @Override
    public ModelMapper<MProductApi, ProductApi, EProductApi, VProductApi> modelMapper() {
        return productApiMapper;
    }

    @Override
    public NameService<EProductApi, Integer> nameService() {
        return productApiService;
    }


    @GetMapping(value = "/product-{productId}", params = {"action", "version"})
    @Override
    public Mono<VProductApi> findByActionAndVersionInProduct(@PathVariable Integer productId,
                                                             @RequestParam("action") String action,
                                                             @RequestParam("version") String version) {
        return Mono.just(productId)
                .map(pId -> productApiService.findOne(productId, action, version).map(this::toVo))
                .flatMap(ReactorUtils::getOrEmpty);
    }

    @GetMapping(value = "/product-res/access-key", params = {"code", "action", "version"})
    @Override
    public Mono<VProductApiAccessDto> getExistsAccessKey(@RequestParam("code") String code, @RequestParam("action") String action, @RequestParam("version") String version) {
        return Mono.just(code)
                .map(c -> productApiAccessService.getProductApiAccessKey(c, action, version).map(
                        dto -> new VProductApiAccessDto().setAccessKeyId(dto.getAccessKeyId())
                                .setAccessKeySecret(dto.getAccessKeySecret())
                                .setRegionId(dto.getRegionId())
                )).flatMap(ReactorUtils::getOrEmpty);
    }

    @PostMapping(value = "/product-res/access-key", params = {"prod-res-id", "prod-api-id", "access-id"})
    @Override
    public Mono<Void> bindAccessKey(@RequestParam("prod-res-id") Integer prodResId, @RequestParam("prod-api-id") Integer apiId, @RequestParam("access-id") Integer accessId) {
        return Mono.just(prodResId)
                .flatMap(ReactorUtils.thenEmpty(i -> {

                    productApiAccessService.bindOrUpdateAccessKey(prodResId, apiId, accessId);
                }));
    }
}
